package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        dishService.save(dishDTO);

        // 清理缓存: 只清除当前菜品分类下的菜品
        String key = "dish+" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询; {}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishService.list(dish);
        return Result.success(list);
    }


    @ApiOperation("删除菜品")
    @DeleteMapping
     public Result<String> delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);

        // 清理缓存:
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
     }

     @ApiOperation("根据id查询菜品")
     @GetMapping("/{id}")
     public Result<DishVO> getDish(@PathVariable Long id) {
        log.info("根据id查询菜品：id", id);
        DishVO dishVO = dishService.getDish(id);
        return Result.success(dishVO);
     }

     @ApiOperation("修改菜品")
     @PutMapping
      public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品 dishDTO : {}", dishDTO);
        dishService.update(dishDTO);

         // 清理缓存:
         Set keys = redisTemplate.keys("dish_*");
         redisTemplate.delete(keys);

        return Result.success();
      }


      @ApiOperation("启售、禁用菜品")
      @PostMapping("/status/{status}")
      public Result<String> startOrStop(@PathVariable Integer status, Long id) {
          log.info("启售、禁用菜品: {} {}", status, id);
          dishService.startOrStop(status, id);

          // 清理缓存
          Set keys = redisTemplate.keys("dish_*");
          redisTemplate.delete(keys);

          return Result.success();
      }


}
